#导包
import smtplib
import tarfile
from email.mime.application import MIMEApplication

import paramiko
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

#打包
def pack():
    tar = tarfile.open("test.tar.gz",mode="w:gz")
    tar.add("report")


def send_email(from_addr,password,to_addr,subject,content,attachment_files=[]):
    msg = MIMEMultipart()
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Subject'] = Header(subject,'utf-8')

    msg.attach(MIMEText(content,'html','utf-8'))
    for attachment in attachment_files:
        att=MIMEApplication(open(attachment,'rb').read(),'gzip')
        att['Content-Type']='application/octet-stream'
        att['Content-Disposition'] = 'attachment; filename="{}"'.format(attachment)
        msg.attach(att)

    try:
        smtp_server='smtp.qq.com'
        with smtplib.SMTP_SSL(smtp_server,465) as smtpobj:
            smtpobj.login(from_addr,password)
            smtpobj.send_message(msg)
            smtpobj.quit()
        print('邮件发送成功')
    except Exception as e:
        print('无法发送邮件',e)

if __name__ == '__main__':
    #pack()
# 发送方邮箱
    from_addr = "3560098310@qq.com"
    password = "rbuizwirlodfciea"

# 收信方邮箱
    to_addr = "handong97@163.com"
#邮件主题和正文
    subject = "这是来自韦天鑫的邮件"
    tar_content = """
    <p>report文件的压缩包</p>
    """

    attachment_files = ['test.tar.gz']

    send_email(from_addr,password,to_addr,subject,tar_content,attachment_files)